package com.qut.graduate.busrtquery.ui.user;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.qut.graduate.busrtquery.BRTQCache;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.BaseActivity;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.adapter.SlidesLineAdapter;
import com.qut.graduate.busrtquery.adapter.SlidesStationAdapter;
import com.qut.graduate.busrtquery.greendao.bean.BusLineCollect;
import com.qut.graduate.busrtquery.greendao.bean.StationInfoCollect;
import com.qut.graduate.busrtquery.greendao.controller.DBController;
import com.qut.graduate.busrtquery.utils.common.LogUtils;
import com.qut.graduate.busrtquery.utils.uiabout.StatusBarUtils;
import com.qut.graduate.busrtquery.view.SlidesRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author WLL
 * @Time: 2019/5/12 20:31
 */
public class CollectActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "CollectActivity";
    private Toolbar toolbar;
    private TextView toolbarTitle;
    private TextView collectTextView;
    private SlidesRecyclerView collectRecyclerView;
    private SlidesLineAdapter slidesLineAdapter;
    private SlidesStationAdapter slidesStationAdapter;
    private List<StationInfoCollect> stationInfoCollectList = new ArrayList<>();
    private List<BusLineCollect> busLineCollectList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collect;
    }

    @Override
    protected void init() {
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbarTitle.setText("收藏中心");
        toolbar.setNavigationIcon(getToolbarIcon(GoogleMaterial.Icon.gmd_arrow_back));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        try {
            collectRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            Intent intent = getIntent();
            if (intent != null) {
                int type = intent.getIntExtra("collectType", 0);
                if (type == BRTQConstant.COLLECT_BUS_LINE) {
                    busLineCollectList.addAll(BRTQCache.getInstance().getBusLineCollectList());
                    if (busLineCollectList.size() == 0) {
                        collectRecyclerView.setVisibility(View.GONE);
                        collectTextView.setVisibility(View.VISIBLE);
                        collectTextView.setText("您还未收藏线路");
                    } else {
                        slidesLineAdapter = new SlidesLineAdapter(mContext, busLineCollectList);
                        slidesLineAdapter.setDeleteListener(new SlidesLineAdapter.OnDeleteListener() {
                            @Override
                            public void onDelete(int position, View view) {
                                slidesLineAdapter.deleteItem(position);
                                collectRecyclerView.removeView(view);
                                LogUtils.d("----CollectActivity busLineCollectList size:" + busLineCollectList.size());
                            }
                        });
                        collectRecyclerView.setAdapter(slidesLineAdapter);
                    }
                } else {
                    stationInfoCollectList.addAll(BRTQCache.getInstance().getStationInfoCollectList());
                    if (stationInfoCollectList.size() == 0) {
                        collectRecyclerView.setVisibility(View.GONE);
                        collectTextView.setVisibility(View.VISIBLE);
                        collectTextView.setText("您还未收藏站点");
                    } else {
                        slidesStationAdapter = new SlidesStationAdapter(mContext, stationInfoCollectList);
                        slidesStationAdapter.setDeleteListener(new SlidesStationAdapter.OnDeleteListener() {
                            @Override
                            public void onDelete(int position, View view) {
                                slidesStationAdapter.deleteItem(position);
                                collectRecyclerView.removeView(view);
                            }
                        });
                        collectRecyclerView.setAdapter(slidesStationAdapter);
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        toolbarTitle = findViewById(R.id.toolbar_title);
        setSupportActionBar(toolbar);
        collectTextView = findViewById(R.id.collect_textView);
        collectRecyclerView = findViewById(R.id.collect_recyclerView);
    }


    @Override
    public void onResume() {
        super.onResume();
        updateNowTheme();
    }

    @SuppressLint("ResourceAsColor")
    private void updateNowTheme() {
        toolbar.setPopupTheme(getPopupToolbarStyle());
        toolbar.setBackgroundColor(getPrimaryColor());
        toolbar.setTitle("");
        StatusBarUtils.fullScreen(this);
        updateBarColor();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {

    }
}
